--
-- Author: shunguo.chen
-- Date: 2019-05-17 16:46:05
--
-- LegendInfo   传奇武器信息
--

local LegendVo = import(".LegendVo")
local LegendStarMap = import(".LegendStarMap")

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

function ClassRef:ctor(data)
    self._isInit = false

    if data then
        self:init(data)
    end
end

function ClassRef:initConf()
    local legends = {}
    local legendConfs = GD:getLegendConfs(UD:getClass())
    for i,conf in ipairs(legendConfs) do
        local legend = LegendVo.new(conf)
        legends[#legends + 1] = legend
    end
    self.legends = legends
end

function ClassRef:isInit()
    return self._isInit
end

-- message LegendInfo{
--     repeated Legend     Legends         = 1;    //神器信息，该数据无序
--     repeated StarMap    StarMpas        = 2;  //星图信息，该数据无序
--     int32 EquipLegendID                 = 3;    //当前装备的神器
-- }
function ClassRef:init(msg)
    if not self._isInit then
        self._isInit = true
        self:initConf()
    end

    self.wearLegendId = msg.EquipLegendID or 0
    self._activeLegendNum = #msg.Legends
    local legendDict = {}
    for i,v in ipairs(msg.Legends or {}) do
        legendDict[v.ID] = v
    end

    for _,legend in ipairs(self.legends) do
        legend:initMsg(legendDict[legend.id])

        -- 穿戴
        if legend.id == self.wearLegendId then
            legend:setWear(true)
            self.wearLegend = legend
        end
    end

    local starMaps = {}
    for i,v in ipairs(msg.StarMpas) do
        local starMap = LegendStarMap.new(v)
        starMaps[#starMaps + 1] = starMap
    end
    table.sort(starMaps, function(a, b)
        return a.id < b.id
    end)

    self.starMaps = starMaps
end

-- 清理技能缓存
function ClassRef:clearSkills()
    for i,legend in ipairs(self.legends) do
        legend:clearSkills()
    end
end

-- 激活新的神器
-- legendMsg pb.Legend 协议文件
function ClassRef:activeLegend(legendMsg)
    for i,legend in ipairs(self.legends) do
        if legend:getId() == legendMsg.ID then
            legend:initMsg(legendMsg)
            legend:setIsNew(true)
            self._activeLegendNum = self._activeLegendNum + 1
            return legend
        end
    end
end

-- legend 穿戴
function ClassRef:updateLegendWear(legend)
    if self.wearLegend == legend then
        local isWear = legend:isWear()
        self.wearLegend:setWear(not isWear)
        if isWear then
            self.wearLegend = nil
        end
    else
        if self.wearLegend then
            self.wearLegend:setWear(false)
        end
        legend:setWear(true)
        self.wearLegend = legend
    end
end

-- starMapIds pb结构 激活的星图ids
function ClassRef:updateStarMapActive(starMapIds)
    for i,id in ipairs(starMapIds) do
        for j,starMap in ipairs(self.starMaps) do
            if starMap:getId() == id then
                starMap:setActive(true)
                break
            end
        end
    end
end

-- 星魂升级
-- message S2C_ActiveLegendStarMapSkill {
--     int32 MapID                 = 1;    //当前星图ID
--     int32 Progress          = 2;    //当前星图完成度万分比
--     bool IsNextActive   = 3;    //下一张星图是否被激活
-- }
function ClassRef:updateStarMapPg(starMap, star, msg)
    star.lv = star.lv + 1
    starMap.pg = msg.Progress/100

    local nextStarMap = self.starMaps[starMap.lv+1]
    if nextStarMap and not nextStarMap:isActive() and msg.IsNextActive then
        nextStarMap:setActive(true)
    end
end

function ClassRef:getWearLegend()
    return self.wearLegend
end

function ClassRef:hasActiveLegend()
    return self._activeLegendNum > 0
end

function ClassRef:getTotalLegendLv()
    local lv = 0
    for i,legend in ipairs(self.legends) do
        if legend:isActive() then
            lv = lv + legend.lv
        end
    end
    return lv
end

-- 获得所有激活星图的总属性
function ClassRef:getShowProps()
    local allPropDict = {}
    local propDict
    for i,starMap in ipairs(self.starMaps) do
        if starMap:isActive() then
            propDict = starMap:getStarPropDict()
            for propId, propValue in pairs(propDict) do
                if allPropDict[propId] then
                    allPropDict[propId] = allPropDict[propId] + propValue
                else
                    allPropDict[propId] = propValue
                end
            end
        end
    end

    local showProps = GD:getLegendStarMapPropShow(UD:getClass())
    for i,attr in ipairs(showProps) do
        local value = allPropDict[attr[1]] or 0
        attr[3] = value
    end

    return showProps
end

-- 获得星图完成进度
-- lv 星图层数
function ClassRef:getStarMapPg(lv)
    local starMap = self.starMaps[lv]
    return starMap.pg
end

-- 获取可操作的星图等级（未100%完成度的星图，从前往后）
function ClassRef:getOperateStarMapLv()
    for i,starMap in ipairs(self.starMaps) do
        if not starMap:isFull() then
            return i
        end
    end
    -- 没找到，用最后一个星图
    return #self.starMaps
end

return ClassRef